<template>
  <el-dialog
    v-model="dialogFormVisible"
    title="查看详情"
    width="800"
    :close-on-click-modal="false"
  >
    <el-form :model="form">
      <el-form-item
        label="手机号："
        :label-width="formLabelWidth"
        style="margin-top: 20px"
      >
        <span>{{ form.phone }}</span>
      </el-form-item>
      <el-form-item label="反馈内容：" :label-width="formLabelWidth">
        <span>{{ form.content }}</span>
      </el-form-item>
      <el-form-item label="反馈类型：" :label-width="formLabelWidth">
        <el-button
          type="primary"
          plain
          v-for="(item, index) in form.type ? form.type.split(',') : []"
          :key="index"
        >
          #{{ buttonLabels[item] }}#
        </el-button>
      </el-form-item>
      <el-form-item label="图片：" :label-width="formLabelWidth">
        <!-- 图片列表 -->
        <div
          class="demo-image__preview"
          v-for="(item, index) in form.imgs ? form.imgs.split(',') : []"
          :key="item"
        >
          <el-image
            style="width: 100px; height: 100px"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :initial-index="currentIndex"
            fit="cover"
            :src="item"
            :preview-src-list="form.imgs ? form.imgs.split(',') : []"
            @click.stop="handleClick(item)"
          />
        </div>
      </el-form-item>

      <el-form-item label="处理状态" :label-width="formLabelWidth">
        <el-radio-group v-model="form.status">
          <el-radio value="1" size="large" :disabled="disabled">
            已处理
          </el-radio>
          <el-radio value="2" size="large" :disabled="disabled">
            未处理
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="处理备注" :label-width="formLabelWidth">
        <el-input
          v-model="form.remake"
          autocomplete="off"
          :disabled="disabled"
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitEvent" :disabled="disabled">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue';
import { getFeedbackSave } from '@/api/operation/operation.js';
import buttonLabels from './buttonLabels.js';
import { ElMessage } from 'element-plus';
const emit = defineEmits(['refreshList']);

const dialogFormVisible = ref(false);
const formLabelWidth = '140px';

const form = ref({});

const currentIndex = ref(0); // 当前预览图片的索引
const disabled = ref(false);

// 点击图片时更新选中的图片索引
const handleClick = (item) => {
  console.log('item:', item);
};

// 打开弹窗
const openChange = (val) => {
  form.value = val;
  form.value.status = form.value.status.toString();
  if (form.value.status === '2') {
    disabled.value = true;
  } else {
    disabled.value = false;
  }
  dialogFormVisible.value = true;
};

// 提交按钮
const submitEvent = () => {
  console.log('form:', form.value);
  dialogFormVisible.value = false;
  getFeedbackSave({
    id: form.value.id,
    status: form.value.status,
    remake: form.value.remake,
  }).then((res) => {
    console.log('res:', res);
    if (res.code === 20000) {
      ElMessage({
        message: '提交成功',
        type: 'success',
      });
      emit('refreshList');
    } else {
      ElMessage({
        message: '提交失败',
        type: 'error',
      });
    }
  });
};
defineExpose({
  openChange,
});
</script>

<style lang="scss" scoped>
.demo-image__preview {
  padding: 10px;
}
</style>
